package com.example.demostudent;

import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/machine")
public class StudentController {

    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;
    @RequestMapping(value = "/loginbyget", method = RequestMethod.POST)
    public void loginByGet(@RequestParam(value = "name", required = true) String name,
                                         @RequestParam(value = "pwd", required = true) String pwd,
                                         @RequestParam(value = "id", required = true) String id
                                                                    ) {
        System.out.println((int)((Math.random()*9+1)*100000000));
        String code = String.valueOf((Math.random()*9+1)*100000000);
        //return studentService.getList(name, pwd,id);
    }
    /**
     * 通过post请求去登陆
     *
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Map<String, String> loginByGet(@RequestBody String requestBody
    ) {
        int num = (int)((Math.random()*9+1)*100000000);
        String code = String.valueOf(num);
        String jsdate="0";
        String id = "0";
        String type="0";
        String name="0";
        String group="0";
        try {
            JSONObject js = new JSONObject(requestBody);
            jsdate = js.getString("regdate");
            String jsdevice = js.getString("device");
            JSONObject device = new JSONObject(jsdevice);
            id =device.getString("id");
            type =device.getString("type");
            name =device.getString("name");
            group =device.getString("group");
            System.out.println(id);
            System.out.println(type);
            System.out.println(name);
            System.out.println(group);

            System.out.println(jsdate);
            System.out.println(jsdevice);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println(code);
        return studentService.getList(id,jsdate,type,group,name,code);
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, String> updateByPost(@RequestBody String requestBody
    ) {
        String jsdate="0";
        String id = "0";
        String type="0";
        String name="0";
        String group="0";
        String code="0";
        try {
            JSONObject js = new JSONObject(requestBody);
            jsdate = js.getString("updatedate");
            code = js.getString("terminalcode");
            String jsdevice = js.getString("device");
            JSONObject device = new JSONObject(jsdevice);
            id =device.getString("id");
            type =device.getString("type");
            name =device.getString("name");
            group =device.getString("group");
            System.out.println(id);
            System.out.println(type);
            System.out.println(name);
            System.out.println(group);

            System.out.println(jsdate);
            System.out.println(jsdevice);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return studentService.upDate(id,type,group,name,jsdate,code);
    }
    @RequestMapping(value = "/concel", method = RequestMethod.POST)
    public Map<String, String> concelByPost(@RequestBody String requestBody
    ) {
        String jsdate="0";
        String code="0";
        try {
            JSONObject js = new JSONObject(requestBody);
            jsdate = js.getString("canceldate");
            code = js.getString("terminalcode");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return studentService.concel(jsdate,code);
    }
}